package com.jimikongjian.service;

import com.jimikongjian.dao.INotification;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.Notification;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.INotificationService;
import com.jimikongjian.service.base.IUserService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/15.
 */
@Service("notificationService")
public class NotificationService implements INotificationService {


    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iNotificationImpl")
    private INotification iNotificationImpl;


    @Autowired
    @Qualifier("userService")
    private IUserService userService;

    /**
     * 创建一个通知消息
     *
     * @param notification
     * @return
     */
    public ResponseMessage createNotification(Notification notification) {
        if (notification.getNotifyId() > 0
                && notification.getNotifyNick() != ""
                && notification.getContent() != ""
                && notification.getNotifiedId() > 0
                && notification.getNotifiedNick() != ""
                && notification.getStatus() == Notification.STATUS_UNREAD){
            iNotificationImpl.insertNotifications(notification);
            return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询从第start位子到start+length位子的所有的通知
     *
     * @param notifiedId
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectNotificationListByNotifiedId(int notifiedId, int start, int length) {
        if (notifiedId > 0 && start >= 0 && length > 0){
            if (userService.hasUser(notifiedId)){
                List<Notification> notificationList = iNotificationImpl.getNotificationListByNotifiedId(notifiedId, start, length);
                for (int i = 0; i < notificationList.size(); i ++){
                    Notification notification = notificationList.get(i);
                    notification.setStatus(1);
                    iNotificationImpl.updateNotifications(notification);
                }
                return responseMessageService.makeMessage(notificationList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询从第start位子到start+length位子的所有的未读通知
     *
     * @param notifiedId
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectUnreadNotificationListByNotifiedId(int notifiedId, int start, int length) {
        if (notifiedId > 0 && start >= 0 && length > 0){
            if (userService.hasUser(notifiedId)){
                List<Notification> notificationList = iNotificationImpl.getUnreadNotificationListByNotifiedId(notifiedId, start, length);
                for (int i = 0; i < notificationList.size(); i ++){
                    Notification notification = notificationList.get(i);
                    notification.setStatus(1);
                    iNotificationImpl.updateNotifications(notification);
                }
                return responseMessageService.makeMessage(notificationList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询被通知者未查看消息的条数
     *
     * @param notifiedId
     * @return
     */
    @Override
    public ResponseMessage selectUnreadCountByNotifiedId(int notifiedId) {
        if (notifiedId > 0){
            if (userService.hasUser(notifiedId)){
                List<Notification> notificationList = iNotificationImpl.getUnreadCountByNotifiedId(notifiedId);
                return responseMessageService.makeMessage(notificationList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 更新一个通知消息
     *
     * @param notification
     * @return
     */
    public ResponseMessage updateNotification(Notification notification) {
        if (notification.getId() >0
                && notification.getStatus() == Notification.STATUS_READ){
            iNotificationImpl.updateNotifications(notification);
            return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }
}
